/**
 * 评估列表初始化
 */
var EvalDic = {
    id: "evalTree",	//表格id
    seItem: null,		//选中的条目
    table: null,
    ztree: null,
    layerIndex: -1,
    assessmentId: 0,
    assessmentName: null
};

EvalDic.onClickSource = function (e, treeId, treeNode) {
    EvalDic.assessmentId = treeNode.id;
    EvalDic.assessmentName = treeNode.name;
    //重置中间区域
    EvalDicItem.seItem = null;
    EvalDicItem.itemId = 0;
    $(".hisui-checkbox").checkbox("uncheck");
    //查询评估条目
    EvalDicItem.search();
    //查询评估条目选项
    EvalDicEntry.search();
};


/**
 * 评估条目管理初始化
 */
var EvalDicItem = {
    id: "EvalDicItemTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1,
    itemId: null,
    itemName: null
};

/**
 * 初始化表格的列
 */
EvalDicItem.initColumn = function () {
    return [
        {
            field: 'selectItem',
            radio: true
        },
        {
            title: '代码',
            field: 'code',
            width: 100,
            align: 'center',
            valign: 'middle'
        },
        {
            title: '描述',
            field: 'description',
            width: 200,
            align: 'center',
            valign: 'middle'
        },
        {
            title: '类型',
            field: 'multipleChoice',
            width: 50,
            align: 'center',
            valign: 'middle',
            formatter:
                function (value, row, index) {
                    if (value == 1) {
                        return "<span class='label label-success'>多选</span>";
                    } else {
                        return "<span class='label label-primary'>单选</span>";
                    }
                }
        }];
};

/**
 * 点击评估条目，加载对应评估条目选项
 */
EvalDicItem.onClickDic = function (obj, $ele, field) {
    EvalDicItem.seItem = obj;
    EvalDicItem.itemId = obj.id;
    EvalDicItem.itemName = obj.description;
    EvalDicEntry.search();
};

/**
 * 检查是否选中
 */
EvalDicItem.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if (selected.length == 0) {
        Sys.info("请先选中表格中的某一记录！");
        return false;
    } else {
        EvalDicItem.seItem = selected[0];
        return true;
    }
};

/**
 * 点击新增评估条目
 */
EvalDicItem.add = function () {
    if (EvalDic.assessmentId) {
        var index = Sys.open('新增评估条目', Sys.backendPath + '/sdc/evaldicitem/to_add', ['520px', '340px'], function (layero, index, iframeWin) {
            $(iframeWin.document).find("#assessmentId").attr("value", EvalDic.assessmentId);
            $(iframeWin.document).find("#assessmentName").attr("value", EvalDic.assessmentName);
        });
    } else {
        Sys.info("请先选中某一评估表！");
        return false;
    }
    this.layerIndex = index;
};

/**
 * 打开查看评估条目详情
 */
EvalDicItem.edit = function (id) {
    var flag = false;
    if (id) {
        flag = true;
    } else if (this.check()) {
        id = this.seItem.id;
        flag = true;
    }
    if (flag) {
        var index = Sys.open('评估条目详情', Sys.backendPath + '/sdc/evaldicitem/to_update/' + id + "/", ['520px', '340px'], function (layero, index, iframeWin) {
            $(iframeWin.document).find("#assessmentName").attr("value", EvalDic.assessmentName);
        });
        this.layerIndex = index;
    }
};


/**
 * 删除评估条目
 */
EvalDicItem.del = function (id, name) {
    var flag = false;
    if (id) {
        flag = true;
    } else if (this.check()) {
        id = this.seItem.id;
        name = this.seItem.name;
        flag = true;
    }
    if (flag) {
        var operation = function () {
            var ajax = new $ax(Sys.backendPath + "/sdc/evaldicitem/delete", function (data) {
                Sys.success("删除成功!");
                EvalDicItem.table.refresh();
            }, function (data) {
                Sys.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("sdcBaseAssessmenitemId", id);
            ajax.start();
        };
        Sys.confirm("是否删除该评估条目?", operation);
    }
};


/**
 * 查询评估条目列表
 */
EvalDicItem.search = function () {
    var queryData = {};
    queryData['assessmentId'] = EvalDic.assessmentId;
    EvalDicItem.table.refresh({query: queryData, pageNumber: 1});
};

/**
 * 评估条目选项管理初始化
 */
var EvalDicEntry = {
    id: "EvalDicEntryTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
EvalDicEntry.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
        {
            field: 'code',
            title: '代码',
            width: 50,
            align: 'center'
        }, {
            field: 'description',
            title: '描述',
            width: 200,
            align: 'center'
        }, {
            field: 'score',
            title: '分值',
            width: 50,
            align: 'center'
        }, {
            field: 'type',
            title: '类型',
            width: 80,
            align: 'center',
            formatter: function (value, row, index) {
                if (value == "input") {
                    return "<span class='label label-success'>输入框</span>";
                } else {
                    return "<span class='label label-primary'>普通DIV</span>";
                }
            }
        }, {
            field: 'formula',
            title: '计算规则',
            width: 200,
            align: 'center'
        }, {
            field: 'mutexFlag',
            title: '互斥标识',
            width: 80,
            align: 'center'
        }, {
            field: 'relationFlag',
            title: '关联标识',
            width: 80,
            align: 'center'
        }];
};


/**
 * 检查是否选中
 */
EvalDicEntry.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if (selected.length == 0) {
        Sys.info("请先选中表格中的某一记录！");
        return false;
    } else {
        EvalDicEntry.seItem = selected[0];
        return true;
    }
};

/**
 * 点击新增评估条目选项
 */
EvalDicEntry.add = function () {
    if (EvalDicItem.seItem) {
        var index = Sys.open('新增评估表条目选项', Sys.backendPath + '/sdc/evaldicentry/to_add', ['520px', '340px'], function (layero, index, iframeWin) {
            $(iframeWin.document).find("#itemId").attr("value", EvalDicItem.itemId);
            $(iframeWin.document).find("#itemName").attr("value", EvalDicItem.itemName);
        });
        this.layerIndex = index;
    } else {
        Sys.info("请先选中某一评估条目！");
        return false;
    }

};

/**
 * 打开查看评估条目选项详情
 */
EvalDicEntry.edit = function (id) {
    var flag = false;
    if (id) {
        flag = true;
    } else if (this.check()) {
        id = this.seItem.id;
        flag = true;
    }
    if (flag) {
        var index = Sys.open('评估条目选项详情', Sys.backendPath + '/sdc/evaldicentry/to_update/' + id + "/", ['520px', '340px'], function (layero, index, iframeWin) {
            $(iframeWin.document).find("#itemName").attr("value", EvalDicItem.itemName);
        });
        this.layerIndex = index;
    }
};

/**
 * 删除评估条目选项
 */
EvalDicEntry.del = function (id, name) {
    var flag = false;
    if (id) {
        flag = true;
    } else if (this.check()) {
        id = this.seItem.id;
        name = this.seItem.name;
        flag = true;
    }
    if (flag) {
        var operation = function () {
            var ajax = new $ax(Sys.backendPath + "/sdc/evaldicentry/delete", function (data) {
                Sys.success("删除成功!");
                EvalDicEntry.table.refresh();
            }, function (data) {
                Sys.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("sdcBaseAssessmenentryId", id);
            ajax.start();
        };
        Sys.confirm("是否删除评估条目选项?", operation);
    }
};

/**
 * 查询评估条目选项列表
 */
EvalDicEntry.search = function () {
    var queryData = {};
    queryData['itemId'] = EvalDicItem.itemId;
    EvalDicEntry.table.refresh({query: queryData, pageNumber: 1});
};

$(function () {
    var assessmentId = ($("#asmtId").val()==undefined)?0:$("#asmtId").val();
    //查询数据源树
    var ztree = new $ZTree("evalTree", Sys.backendPath + "/sdc/evaldic/tree");
    ztree.bindOnClick(EvalDic.onClickSource);
    ztree.init();
    EvalDic.ztree = ztree;
    fuzzySearch('evalTree', '#seachKey', null, true);
    //查询并初始化评估条目表格
    var itemColunms = EvalDicItem.initColumn();
    var itemtable = new BSTable(EvalDicItem.id, Sys.backendPath + "/sdc/evaldicitem/list", itemColunms);
    itemtable.setPaginationType("server");
    itemtable.setShowExport(false);
    itemtable.setShowRefresh(false);
    itemtable.setShowToggle(false);
    itemtable.setShowColumns(false);
    itemtable.setOnClickRow(EvalDicItem.onClickDic);
    itemtable.setPageSize(12);
    itemtable.setQueryParams({"assessmentId": assessmentId});
    EvalDicItem.table = itemtable.init();
    //查询并初始化选项表格
    var entryColunms = EvalDicEntry.initColumn();
    var entrytable = new BSTable(EvalDicEntry.id, Sys.backendPath + "/sdc/evaldicentry/list", entryColunms);
    entrytable.setPaginationType("server");
    entrytable.setShowExport(false);
    entrytable.setShowRefresh(false);
    entrytable.setShowToggle(false);
    entrytable.setShowColumns(false);
    entrytable.setPageSize(12);
    entrytable.setQueryParams({"itemId": 0});
    EvalDicEntry.table = entrytable.init();

    if (assessmentId != undefined) {
        var treeObj = $.fn.zTree.getZTreeObj("evalTree");//ztree树的ID
        var node = treeObj.getNodeByParam("id", assessmentId);//根据ID找到该节点
        treeObj.selectNode(node);//根据该节点选中
        if(node){
            //触发默认数据的click事件
            $("#"+node.tId+"_a").click();//触发ztree点击事件
        }
    }
});